import * as api from '@packages/lib-api';
import PropTypes from 'prop-types';
import React from 'react';
import { connect, dispatch } from 'tva';

import './banner.model';

const API = api.request({
    // 获取 banner 的数据
    banner: '/v1.0/index/init-index-banner-icon',
});

/**
 * 首页共享数据
 * banner 头图
 * module 8大模块: 名家精选, 优店
 */

class RenderBanner extends React.Component {
    static propTypes = {
        onGetBanner: PropTypes.func,
        render: PropTypes.func.isRequired,
        homeHead: PropTypes.object,
    };

    componentDidMount() {
        this.initBanner();
    }

    // 获取banner
    async initBanner() {
        const { data } = await API.banner.get();
        this.props.onGetBanner && this.props.onGetBanner(data);

        dispatch({
            type: 'homeHead/updateState',
            data,
        });
    }

    render() {
        return this.props.render(this.props.homeHead);
    }
}

export default connect({
    selector: ({ homeHead }) => ({ homeHead }),
})(RenderBanner);
